home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sunrpc / svc.c < prev    next >
C/C++ Source or Header  |  1994-02-06  |  11KB  |  480 lines

  1. /* @(#)svc.c    2.4 88/08/11 4.0 RPCSRC; from 1.44 88/02/08 SMI */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)svc.c 1.41 87/10/13 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /*
  35.  * svc.c, Server-side remote procedure call interface.
  36.  *
  37.  * There are two sets of procedures here.  The xprt routines are
  38.  * for handling transport handles.  The svc routines handle the
  39.  * list of service routines.
  40.  *
  41.  * Copyright (C) 1984, Sun Microsystems, Inc.
  42.  */
  43.  
  44. #include <sys/errno.h>
  45. #include <rpc/rpc.h>
  46. #include <rpc/pmap_clnt.h>
  47.  
  48. extern int errno;
  49.  
  50. #ifdef FD_SETSIZE
  51. static SVCXPRT **xports;
  52. #else
  53. #define NOFILE 32
  54.  
  55. static SVCXPRT *xports[NOFILE];
  56. #endif /* def FD_SETSIZE */
  57.  
  58. #define NULL_SVC ((struct svc_callout *)0)
  59. #define    RQCRED_SIZE    400        /* this size is excessive */
  60.  
  61. /*
  62.  * The services list
  63.  * Each entry represents a set of procedures (an rpc program).
  64.  * The dispatch routine takes request structs and runs the
  65.  * apropriate procedure.
  66.  */
  67. static struct svc_callout {
  68.     struct svc_callout *sc_next;
  69.     u_long            sc_prog;
  70.     u_long            sc_vers;
  71.     void            (*sc_dispatch)();
  72. } *svc_head;
  73.  
  74. static struct svc_callout *svc_find();
  75.  
  76. /* ***************  SVCXPRT related stuff **************** */
  77.  
  78. /*
  79.  * Activate a transport handle.
  80.  */
  81. void
  82. xprt_register(xprt)
  83.     SVCXPRT *xprt;
  84. {
  85.     register int sock = xprt->xp_sock;
  86.  
  87. #ifdef FD_SETSIZE
  88.     if (xports == NULL) {
  89.         xports = (SVCXPRT **)
  90.             mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
  91.     }
  92.     if (sock < _rpc_dtablesize()) {
  93.         xports[sock] = xprt;
  94.         FD_SET(sock, &svc_fdset);
  95.     }
  96. #else
  97.     if (sock < NOFILE) {
  98.         xports[sock] = xprt;
  99.         svc_fds |= (1 << sock);
  100.     }
  101. #endif /* def FD_SETSIZE */
  102.  
  103. }
  104.  
  105. /*
  106.  * De-activate a transport handle. 
  107.  */
  108. void
  109. xprt_unregister(xprt) 
  110.     SVCXPRT *xprt;
  111.     register int sock = xprt->xp_sock;
  112.  
  113. #ifdef FD_SETSIZE
  114.     if ((sock < _rpc_dtablesize()) && (xports[sock] == xprt)) {
  115.         xports[sock] = (SVCXPRT *)0;
  116.         FD_CLR(sock, &svc_fdset);
  117.     }
  118. #else
  119.     if ((sock < NOFILE) && (xports[sock] == xprt)) {
  120.         xports[sock] = (SVCXPRT *)0;
  121.         svc_fds &= ~(1 << sock);
  122.     }
  123. #endif /* def FD_SETSIZE */
  124. }
  125.  
  126.  
  127. /* ********************** CALLOUT list related stuff ************* */
  128.  
  129. /*
  130.  * Add a service program to the callout list.
  131.  * The dispatch routine will be called when a rpc request for this
  132.  * program number comes in.
  133.  */
  134. bool_t
  135. svc_register(xprt, prog, vers, dispatch, protocol)
  136.     SVCXPRT *xprt;
  137.     u_long prog;
  138.     u_long vers;
  139.     void (*dispatch)();
  140.     int protocol;
  141. {
  142.     struct svc_callout *prev;
  143.     register struct svc_callout *s;
  144.  
  145.     if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) {
  146.         if (s->sc_dispatch == dispatch)
  147.             goto pmap_it;  /* he is registering another xptr */
  148.         return (FALSE);
  149.     }
  150.     s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout));
  151.     if (s == (struct svc_callout *)0) {
  152.         return (FALSE);
  153.     }
  154.     s->sc_prog = prog;
  155.     s->sc_vers = vers;
  156.     s->sc_dispatch = dispatch;
  157.     s->sc_next = svc_head;
  158.     svc_head = s;
  159. pmap_it:
  160.     /* now register the information with the local binder service */
  161.     if (protocol) {
  162.         return (pmap_set(prog, vers, protocol, xprt->xp_port));
  163.     }
  164.     return (TRUE);
  165. }
  166.  
  167. /*
  168.  * Remove a service program from the callout list.
  169.  */
  170. void
  171. svc_unregister(prog, vers)
  172.     u_long prog;
  173.     u_long vers;
  174. {
  175.     struct svc_callout *prev;
  176.     register struct svc_callout *s;
  177.  
  178.     if ((s = svc_find(prog, vers, &prev)) == NULL_SVC)
  179.         return;
  180.     if (prev == NULL_SVC) {
  181.         svc_head = s->sc_next;
  182.     } else {
  183.         prev->sc_next = s->sc_next;
  184.     }
  185.     s->sc_next = NULL_SVC;
  186.     mem_free((char *) s, (u_int) sizeof(struct svc_callout));
  187.     /* now unregister the information with the local binder service */
  188.     (void)pmap_unset(prog, vers);
  189. }
  190.  
  191. /*
  192.  * Search the callout list for a program number, return the callout
  193.  * struct.
  194.  */
  195. static struct svc_callout *
  196. svc_find(prog, vers, prev)
  197.     u_long prog;
  198.     u_long vers;
  199.     struct svc_callout **prev;
  200. {
  201.     register struct svc_callout *s, *p;
  202.  
  203.     p = NULL_SVC;
  204.     for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
  205.         if ((s->sc_prog == prog) && (s->sc_vers == vers))
  206.             goto done;
  207.         p = s;
  208.     }
  209. done:
  210.     *prev = p;
  211.     return (s);
  212. }
  213.  
  214. /* ******************* REPLY GENERATION ROUTINES  ************ */
  215.  
  216. /*
  217.  * Send a reply to an rpc request
  218.  */
  219. bool_t
  220. svc_sendreply(xprt, xdr_results, xdr_location)
  221.     register SVCXPRT *xprt;
  222.     xdrproc_t xdr_results;
  223.     caddr_t xdr_location;
  224. {
  225.     struct rpc_msg rply; 
  226.  
  227.     rply.rm_direction = REPLY;  
  228.     rply.rm_reply.rp_stat = MSG_ACCEPTED; 
  229.     rply.acpted_rply.ar_verf = xprt->xp_verf; 
  230.     rply.acpted_rply.ar_stat = SUCCESS;
  231.     rply.acpted_rply.ar_results.where = xdr_location;
  232.     rply.acpted_rply.ar_results.proc = xdr_results;
  233.     return (SVC_REPLY(xprt, &rply)); 
  234. }
  235.  
  236. /*
  237.  * No procedure error reply
  238.  */
  239. void
  240. svcerr_noproc(xprt)
  241.     register SVCXPRT *xprt;
  242. {
  243.     struct rpc_msg rply;
  244.  
  245.     rply.rm_direction = REPLY;
  246.     rply.rm_reply.rp_stat = MSG_ACCEPTED;
  247.     rply.acpted_rply.ar_verf = xprt->xp_verf;
  248.     rply.acpted_rply.ar_stat = PROC_UNAVAIL;
  249.     SVC_REPLY(xprt, &rply);
  250. }
  251.  
  252. /*
  253.  * Can't decode args error reply
  254.  */
  255. void
  256. svcerr_decode(xprt)
  257.     register SVCXPRT *xprt;
  258. {
  259.     struct rpc_msg rply; 
  260.  
  261.     rply.rm_direction = REPLY; 
  262.     rply.rm_reply.rp_stat = MSG_ACCEPTED; 
  263.     rply.acpted_rply.ar_verf = xprt->xp_verf;
  264.     rply.acpted_rply.ar_stat = GARBAGE_ARGS;
  265.     SVC_REPLY(xprt, &rply); 
  266. }
  267.  
  268. /*
  269.  * Some system error
  270.  */
  271. void
  272. svcerr_systemerr(xprt)
  273.     register SVCXPRT *xprt;
  274. {
  275.     struct rpc_msg rply; 
  276.  
  277.     rply.rm_direction = REPLY; 
  278.     rply.rm_reply.rp_stat = MSG_ACCEPTED; 
  279.     rply.acpted_rply.ar_verf = xprt->xp_verf;
  280.     rply.acpted_rply.ar_stat = SYSTEM_ERR;
  281.     SVC_REPLY(xprt, &rply); 
  282. }
  283.  
  284. /*
  285.  * Authentication error reply
  286.  */
  287. void
  288. svcerr_auth(xprt, why)
  289.     SVCXPRT *xprt;
  290.     enum auth_stat why;
  291. {
  292.     struct rpc_msg rply;
  293.  
  294.     rply.rm_direction = REPLY;
  295.     rply.rm_reply.rp_stat = MSG_DENIED;
  296.     rply.rjcted_rply.rj_stat = AUTH_ERROR;
  297.     rply.rjcted_rply.rj_why = why;
  298.     SVC_REPLY(xprt, &rply);
  299. }
  300.  
  301. /*
  302.  * Auth too weak error reply
  303.  */
  304. void
  305. svcerr_weakauth(xprt)
  306.     SVCXPRT *xprt;
  307. {
  308.  
  309.     svcerr_auth(xprt, AUTH_TOOWEAK);
  310. }
  311.  
  312. /*
  313.  * Program unavailable error reply
  314.  */
  315. void 
  316. svcerr_noprog(xprt)
  317.     register SVCXPRT *xprt;
  318. {
  319.     struct rpc_msg rply;  
  320.  
  321.     rply.rm_direction = REPLY;   
  322.     rply.rm_reply.rp_stat = MSG_ACCEPTED;  
  323.     rply.acpted_rply.ar_verf = xprt->xp_verf;  
  324.     rply.acpted_rply.ar_stat = PROG_UNAVAIL;
  325.     SVC_REPLY(xprt, &rply);
  326. }
  327.  
  328. /*
  329.  * Program version mismatch error reply
  330.  */
  331. void  
  332. svcerr_progvers(xprt, low_vers, high_vers)
  333.     register SVCXPRT *xprt; 
  334.     u_long low_vers;
  335.     u_long high_vers;
  336. {
  337.     struct rpc_msg rply;
  338.  
  339.     rply.rm_direction = REPLY;
  340.     rply.rm_reply.rp_stat = MSG_ACCEPTED;
  341.     rply.acpted_rply.ar_verf = xprt->xp_verf;
  342.     rply.acpted_rply.ar_stat = PROG_MISMATCH;
  343.     rply.acpted_rply.ar_vers.low = low_vers;
  344.     rply.acpted_rply.ar_vers.high = high_vers;
  345.     SVC_REPLY(xprt, &rply);
  346. }
  347.  
  348. /* ******************* SERVER INPUT STUFF ******************* */
  349.  
  350. /*
  351.  * Get server side input from some transport.
  352.  *
  353.  * Statement of authentication parameters management:
  354.  * This function owns and manages all authentication parameters, specifically
  355.  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
  356.  * the "cooked" credentials (rqst->rq_clntcred).
  357.  * However, this function does not know the structure of the cooked
  358.  * credentials, so it make the following assumptions: 
  359.  *   a) the structure is contiguous (no pointers), and
  360.  *   b) the cred structure size does not exceed RQCRED_SIZE bytes. 
  361.  * In all events, all three parameters are freed upon exit from this routine.
  362.  * The storage is trivially management on the call stack in user land, but
  363.  * is mallocated in kernel land.
  364.  */
  365.  
  366. void
  367. svc_getreq(rdfds)
  368.     int rdfds;
  369. {
  370. #ifdef FD_SETSIZE
  371.     fd_set readfds;
  372.  
  373.     FD_ZERO(&readfds);
  374.     readfds.fds_bits[0] = rdfds;
  375.     svc_getreqset(&readfds);
  376. #else
  377.     int readfds = rdfds & svc_fds;
  378.  
  379.     svc_getreqset(&readfds);
  380. #endif /* def FD_SETSIZE */
  381. }
  382.  
  383. void
  384. svc_getreqset(readfds)
  385. #ifdef FD_SETSIZE
  386.     fd_set *readfds;
  387. {
  388. #else
  389.     int *readfds;
  390. {
  391.     int readfds_local = *readfds;
  392. #endif /* def FD_SETSIZE */
  393.     enum xprt_stat stat;
  394.     struct rpc_msg msg;
  395.     int prog_found;
  396.     u_long low_vers;
  397.     u_long high_vers;
  398.     struct svc_req r;
  399.     register SVCXPRT *xprt;
  400.     register u_long mask;
  401.     register int bit;
  402.     register u_long *maskp;
  403.     register int setsize;
  404.     register int sock;
  405.     char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
  406.     msg.rm_call.cb_cred.oa_base = cred_area;
  407.     msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
  408.     r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
  409.  
  410.  
  411. #ifdef FD_SETSIZE
  412.     setsize = _rpc_dtablesize();    
  413.     maskp = (u_long *)readfds->fds_bits;
  414.     for (sock = 0; sock < setsize; sock += NFDBITS) {
  415.         for (mask = *maskp++; bit = ffs(mask); mask ^= (1 << (bit - 1))) {
  416.         /* sock has input waiting */
  417.         xprt = xports[sock + bit - 1];
  418. #else
  419.     for (sock = 0; readfds_local != 0; sock++, readfds_local >>= 1) {
  420.         if ((readfds_local & 1) != 0) {
  421.         /* sock has input waiting */
  422.         xprt = xports[sock];
  423. #endif /* def FD_SETSIZE */
  424.         /* now receive msgs from xprtprt (support batch calls) */
  425.         do {
  426.             if (SVC_RECV(xprt, &msg)) {
  427.  
  428.                 /* now find the exported program and call it */
  429.                 register struct svc_callout *s;
  430.                 enum auth_stat why;
  431.  
  432.                 r.rq_xprt = xprt;
  433.                 r.rq_prog = msg.rm_call.cb_prog;
  434.                 r.rq_vers = msg.rm_call.cb_vers;
  435.                 r.rq_proc = msg.rm_call.cb_proc;
  436.                 r.rq_cred = msg.rm_call.cb_cred;
  437.                 /* first authenticate the message */
  438.                 if ((why= _authenticate(&r, &msg)) != AUTH_OK) {
  439.                     svcerr_auth(xprt, why);
  440.                     goto call_done;
  441.                 }
  442.                 /* now match message with a registered service*/
  443.                 prog_found = FALSE;
  444.                 low_vers = 0 - 1;
  445.                 high_vers = 0;
  446.                 for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
  447.                     if (s->sc_prog == r.rq_prog) {
  448.                         if (s->sc_vers == r.rq_vers) {
  449.                             (*s->sc_dispatch)(&r, xprt);
  450.                             goto call_done;
  451.                         }  /* found correct version */
  452.                         prog_found = TRUE;
  453.                         if (s->sc_vers < low_vers)
  454.                             low_vers = s->sc_vers;
  455.                         if (s->sc_vers > high_vers)
  456.                             high_vers = s->sc_vers;
  457.                     }   /* found correct program */
  458.                 }
  459.                 /*
  460.                  * if we got here, the program or version
  461.                  * is not served ...
  462.                  */
  463.                 if (prog_found)
  464.                     svcerr_progvers(xprt,
  465.                     low_vers, high_vers);
  466.                 else
  467.                      svcerr_noprog(xprt);
  468.                 /* Fall through to ... */
  469.             }
  470.         call_done:
  471.             if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
  472.                 SVC_DESTROY(xprt);
  473.                 break;
  474.             }
  475.         } while (stat == XPRT_MOREREQS);
  476.         }
  477.     }
  478. }
  479.